<template>
	<uni-section title="厂家" type="line">
		<template v-slot:right>
			<input type="text" placeholder="请输入厂家" class="inputCj" v-model='cj'>
		</template>
	</uni-section>
	<uni-section title="组别" type="line">
		<uni-data-select class="sel" v-model="tabFir" :localdata="tabFirList" :clear="false" @change="changeTabSec" ></uni-data-select>
		<uni-data-select class="sel" v-model="tabSec" :localdata="tabSecList" :clear="false" ></uni-data-select>
	</uni-section>
	<uni-section title="商品照片" type="line">
		<view class="example-body">
			<uni-file-picker limit="9" ref='imgPicker'></uni-file-picker>
		</view>
	</uni-section>
	<button class="btn" @click="submit">提交</button>
</template>

<script>
	import path from "path/win32"
import config from "../../../utils/config.js"
	export default{
		onLoad(){
			this.getTabListData()
		},
		data(){
			return {
				cj:'',
				tabList:{},
				tabFir:'',
				tabSec:'',
				tabFirList:[],
				tabSecList:[],
			}
		},
		methods:{
			// 获取组别信息
			getTabListData(){
				let that = this
				this.request({
					url: config.baseUrl + "/wx/getTabListData",
				}).then(res => {
					that.tabList = {}
					let data = res.data
					for (let i =0;i<data.length;i++){
						if (data[i].aName in that.tabList){
							that.tabList[data[i]['aName']].push(data[i]['bName'])
						}else{
							that.tabList[data[i]['aName']] = [data[i]['bName']]
						}
					}
					for (let key in that.tabList){
						that.tabFirList.push({'text': key, 'value': key})
					}
				})
			},
			// 修改第二组别
			changeTabSec(){
				this.tabSecList = []
				let data = this.tabList[this.tabFir]
				for (let i=0;i<data.length;i++){
					this.tabSecList.push({'text': data[i], 'value': data[i]})
				}
			},
			// 上传
			submit(){
				if (this.cj === '' || this.tabFir === '' || this.tabSec === ''){
					uni.showToast({
						title: '数据不全',
						icon: 'none',
						//显示持续时间为 2秒
						duration: 2000
					})
				}else{
					let data = this.$refs.imgPicker.files
					for (let i = 0;i<data.length;i++){
						let that = this
						uni.uploadFile({
							url: config.baseUrl + '/wx/uploadNewSpPic', 
							filePath: data[i]['path'],
							name: 'file',
							formData: {
								'cj': that.cj,
								'tabFir': that.tabFir,
								'tabSec': that.tabSec,
							},
							success: (res) => {
								that.$refs.imgPicker.files = []
							}
						});
					}
				}

			},
			
		}
	}
</script>

<style>
	.inputCj{
		float: right;
		text-align: right;
	}
	.sel{
		width: 45%;
		margin: 2.5%;
		float: left;
	}
</style>